import 'package:flutter/material.dart';

class DateViewExample extends StatefulWidget {
  const DateViewExample({super.key});

  @override
  State<DateViewExample> createState() => _DateViewExampleState();
}

class _DateViewExampleState extends State<DateViewExample> {
  @override
  void initState() {
    super.initState();
    /**
        获取日期：获取当前时间，获取时分秒
     * */
    DateTime d = DateTime.now();
    print(d); //2023-12-31 18:02:54.152797
    print(
        "${d.year}-${d.month}-${d.day} ${d.hour}:${d.minute}:${d.second}"); // 2023-12-31 18:2:54
    //获取时间戳
    print(d.millisecondsSinceEpoch); //获取时间戳:默认十三位时间戳
  }

  DateTime _datetime = DateTime.now();
  TimeOfDay _time = TimeOfDay.now();
  @override
  Widget build(BuildContext context) {
    //日期转换成时间戳
    DateTime d1 = DateTime(2023, 12, 31); //创建 日期
    print('日期转时间戳:${d1.millisecondsSinceEpoch}'); //日期转时间戳

    //日期字符串转换成时间
    String date1 = '2021-08-01';
    String date2 = '2023-12-31 18:14:30';
    print('日期字符串转时间date1：${DateTime.parse(date1)}');
    print('日期字符串转时间date2：${DateTime.parse(date2)}');
    String date3 = '2023-12-31 18:14:30';
    print('日期字符串转时间戳，间接转date3：${DateTime.parse(date2).millisecondsSinceEpoch}');

    // 时间戳转换成日期
    DateTime d2 = DateTime(2023, 12, 31, 18, 22, 22);
    int unixtime = d2.millisecondsSinceEpoch;
    print('时间戳 转成日期:${DateTime.fromMillisecondsSinceEpoch(unixtime)}');

    //时间加减
    DateTime time = DateTime.now();
    print('当前时间:${time}');
    print('分钟+30 时间${time.add(const Duration(minutes: 30))}');
    print('分钟-30 时间:${time.add(const Duration(minutes: -30))}');
    print('小时+10：${time.add(const Duration(hours: 10))}');
    return Scaffold(
        appBar: AppBar(
          title: Text('时间'),
        ),
        body: Column(
          children: [
            InkWell(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text("${_datetime.year}-${_datetime.month}-${_datetime.day}"),
                  const Icon(Icons.arrow_drop_down)
                ],
              ),
              onTap: () async {
                print('onTap  点击事件！');
                DateTime? time = await showDatePicker(
                  context: context,
                  initialDate: _datetime,
                  firstDate: DateTime(1900),
                  lastDate: DateTime(2050),
                  locale: const Locale('zh'),
                );
                if (time != null) {
                  setState(() {
                    _datetime = time;
                  });
                }
              },
            ),
            SizedBox(height: 20),
            InkWell(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text("${_time.hour}:${_time.minute}"),
                  const Icon(Icons.arrow_drop_down)
                ],
              ),
              onTap: () async {
                TimeOfDay? time =
                    await showTimePicker(context: context, initialTime: _time);
                if (time != null) {
                  setState(() {
                    _time = time;
                  });
                }
              },
            )
          ],
        ));
  }
}
